package cn.blb.service.vod.service.impl;

import cn.blb.service.vod.service.VodService;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Service
public class VodServiceImpl implements VodService{
    private cn.blb.service.vod.util.PropUtil propUtil;

    public VodServiceImpl(cn.blb.service.vod.util.PropUtil propUtil) {
        this.propUtil = propUtil;
    }

    @Override
    public String upload(MultipartFile file) {

            // title fileName inputStream
            String fileName = file.getOriginalFilename();
            // 截取title
            String title = fileName.substring(0, fileName.lastIndexOf("."));
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 获取request
            UploadStreamRequest request = new UploadStreamRequest(propUtil.getKeyid(),
                    propUtil.getKeysecret(), title, fileName, inputStream);
            // 添加region
            request.setApiRegionId("cn-shanghai");
            // 添加分类id
            // request.setCateId(1000388267L);
            UploadVideoImpl uploader = new UploadVideoImpl();
            // 获取结果
            UploadStreamResponse response = uploader.uploadStream(request);

            return response.getVideoId();

    }
}
